<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据库设计与优化 - 专业指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
        }
        .hero {
            background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%);
        }
        .section-title {
            position: relative;
            padding-left: 1.5rem;
        }
        .section-title:before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.4em;
            height: 1.2em;
            width: 0.5rem;
            background: #3b82f6;
            border-radius: 0.25rem;
        }
        .card {
            transition: all 0.3s ease;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        .card:hover {
            transform: translateY(-0.25rem);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .hover-scale {
            transition: all 0.3s ease;
        }
        .hover-scale:hover {
            transform: scale(1.03);
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 0.8;
            margin: 0.2em 0.5rem 0 0;
            color: #1e40af;
            font-weight: bold;
        }
        .timeline-item:before {
            content: '';
            position: absolute;
            left: 1.5rem;
            top: 0;
            bottom: 0;
            width: 2px;
            background: #e5e7eb;
        }
        .timeline-dot {
            position: absolute;
            left: 1.25rem;
            top: 0.75rem;
            width: 0.5rem;
            height: 0.5rem;
            border-radius: 50%;
            background: #3b82f6;
            border: 3px solid white;
            box-shadow: 0 0 0 2px #3b82f6;
        }
        .glow-text {
            text-shadow: 0 0 8px rgba(59, 130, 246, 0.3);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 glow-text">数据库设计与优化</h1>
                <p class="text-xl md:text-2xl max-w-3xl mb-8 opacity-90">构建高效、可扩展和高可用系统的关键策略与技术</p>
                <div class="flex space-x-4">
                    <a href="#introduction" class="bg-white text-blue-600 hover:bg-blue-50 px-6 py-3 rounded-lg font-medium transition duration-300 hover:shadow-lg">
                        <i class="fas fa-book-open mr-2"></i>开始阅读
                    </a>
                    <a href="#principles" class="border-2 border-white text-white hover:bg-white hover:text-blue-600 px-6 py-3 rounded-lg font-medium transition duration-300">
                        <i class="fas fa-lightbulb mr-2"></i>设计原则
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section id="introduction" class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="bg-white rounded-xl p-8 shadow-lg">
                <p class="text-lg leading-relaxed drop-cap">
                    数据库设计与优化是开发高效、可扩展和高可用系统的关键。随着数据量的增加，系统性能的要求也越来越高，合理的数据库设计和优化可以显著提高系统的效率，减少资源消耗，降低响应时间。
                </p>
                <div class="mt-8 grid md:grid-cols-2 gap-8">
                    <div class="card bg-gradient-to-br from-blue-50 to-white p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-3 rounded-full mr-4">
                                <i class="fas fa-database text-blue-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">为什么重要</h3>
                        </div>
                        <p class="text-gray-700">良好的数据库设计可以提高查询效率50%以上，减少服务器负载，提升用户体验。</p>
                    </div>
                    <div class="card bg-gradient-to-br from-purple-50 to-white p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-3 rounded-full mr-4">
                                <i class="fas fa-chart-line text-purple-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">性能提升</h3>
                        </div>
                        <p class="text-gray-700">合理的索引设计可以使查询速度提升10-100倍，显著降低响应时间。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Principles Section -->
    <section id="principles" class="py-16 px-4 bg-gray-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">数据库设计的基本原则</h2>
            <p class="text-lg text-gray-700 mb-8 text-center max-w-3xl mx-auto">
                数据库设计的目标是确保系统的数据能够高效地存储、管理和访问。设计良好的数据库不仅能保证数据的完整性与一致性，还能够支持高效的查询和扩展性。
            </p>
            
            <div class="grid md:grid-cols-3 gap-8 mb-16">
                <div class="card bg-white p-6 hover-scale">
                    <div class="text-blue-600 text-4xl mb-4">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">正确建模数据</h3>
                    <p class="text-gray-600">将业务需求转化为概念模型、逻辑模型和物理模型，确保数据结构合理。</p>
                </div>
                <div class="card bg-white p-6 hover-scale">
                    <div class="text-purple-600 text-4xl mb-4">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">规范化与去规范化</h3>
                    <p class="text-gray-600">平衡数据完整性与查询性能，根据场景选择适当的范式级别。</p>
                </div>
                <div class="card bg-white p-6 hover-scale">
                    <div class="text-green-600 text-4xl mb-4">
                        <i class="fas fa-broom"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">避免数据冗余</h3>
                    <p class="text-gray-600">通过合理的表设计和索引策略，减少存储浪费和一致性问题。</p>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg mb-12">
                <h3 class="text-2xl font-semibold mb-6 section-title">数据建模的三个阶段</h3>
                <div class="relative">
                    <div class="timeline-item pl-12 pb-8 relative">
                        <div class="timeline-dot"></div>
                        <h4 class="text-xl font-semibold mb-2">概念模型</h4>
                        <p class="text-gray-600">关注业务实体和关系，使用ER图等工具描述业务需求。</p>
                    </div>
                    <div class="timeline-item pl-12 pb-8 relative">
                        <div class="timeline-dot"></div>
                        <h4 class="text-xl font-semibold mb-2">逻辑模型</h4>
                        <p class="text-gray-600">将概念模型转化为表结构，定义字段、主键、外键等。</p>
                    </div>
                    <div class="timeline-item pl-12 relative">
                        <div class="timeline-dot"></div>
                        <h4 class="text-xl font-semibold mb-2">物理模型</h4>
                        <p class="text-gray-600">考虑具体DBMS特性，优化存储结构、索引和分区策略。</p>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">数据库规范化</h3>
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">范式</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">要求</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">优势</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">1NF</td>
                                <td class="px-6 py-4 text-sm text-gray-500">消除重复组，确保原子性</td>
                                <td class="px-6 py-4 text-sm text-gray-500">基础数据结构</td>
                                <td class="px-6 py-4 text-sm text-gray-500">所有关系数据库</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">2NF</td>
                                <td class="px-6 py-4 text-sm text-gray-500">消除部分依赖</td>
                                <td class="px-6 py-4 text-sm text-gray-500">减少更新异常</td>
                                <td class="px-6 py-4 text-sm text-gray-500">多字段主键表</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">3NF</td>
                                <td class="px-6 py-4 text-sm text-gray-500">消除传递依赖</td>
                                <td class="px-6 py-4 text-sm text-gray-500">提高数据一致性</td>
                                <td class="px-6 py-4 text-sm text-gray-500">事务型系统</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>

    <!-- Architecture Section -->
    <section id="architecture" class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">数据库架构设计</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">分库分表策略</h3>
                    <div class="mermaid mb-4">
                        graph TD
                            A[原始大表] --> B((水平分片))
                            A --> C((垂直分片))
                            B --> D[表1:按ID范围分]
                            B --> E[表2:按ID范围分]
                            C --> F[表A:用户基础信息]
                            C --> G[表B:用户扩展信息]
                    </div>
                    <p class="text-gray-600">
                        随着数据量的增长，单一的数据库可能无法满足性能需求。分库分表是一种常见的解决方案，它将数据划分到多个数据库或表中，从而减少单一数据库的负载。
                    </p>
                </div>
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">主从复制架构</h3>
                    <div class="mermaid mb-4">
                        graph LR
                            Master[主库:写操作] -->|数据同步| Slave1[从库1:读操作]
                            Master -->|数据同步| Slave2[从库2:读操作]
                            Master -->|数据同步| Slave3[从库3:读操作]
                    </div>
                    <p class="text-gray-600">
                        通过读写分离，将查询负载分配到多个从库上，减轻主库的负担，提高系统的并发能力。该方案在提高查询性能的同时，还增强了系统的容错能力。
                    </p>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">分布式数据库架构对比</h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="border border-gray-200 rounded-lg p-6 hover:border-blue-300 transition">
                        <h4 class="text-lg font-semibold mb-3 flex items-center">
                            <i class="fas fa-server text-blue-500 mr-2"></i> 分片集群
                        </h4>
                        <ul class="text-gray-600 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>水平扩展能力强</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>适合大数据量</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-circle text-yellow-500 mt-1 mr-2"></i>
                                <span>跨分片查询复杂</span>
                            </li>
                        </ul>
                    </div>
                    <div class="border border-gray-200 rounded-lg p-6 hover:border-purple-300 transition">
                        <h4 class="text-lg font-semibold mb-3 flex items-center">
                            <i class="fas fa-network-wired text-purple-500 mr-2"></i> 主从集群
                        </h4>
                        <ul class="text-gray-600 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>读写分离</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>高可用性</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-circle text-yellow-500 mt-1 mr-2"></i>
                                <span>写性能受限于主库</span>
                            </li>
                        </ul>
                    </div>
                    <div class="border border-gray-200 rounded-lg p-6 hover:border-green-300 transition">
                        <h4 class="text-lg font-semibold mb-3 flex items-center">
                            <i class="fas fa-asterisk text-green-500 mr-2"></i> 多主集群
                        </h4>
                        <ul class="text-gray-600 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>写性能高</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>无单点故障</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-circle text-yellow-500 mt-1 mr-2"></i>
                                <span>数据冲突需要解决</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Indexing Section -->
    <section id="indexing" class="py-16 px-4 bg-gray-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">数据库索引优化</h2>
            
            <div class="bg-white rounded-xl p-8 shadow-lg mb-12">
                <h3 class="text-2xl font-semibold mb-6 section-title">索引类型对比</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <div class="flex items-center mb-3">
                            <div class="bg-blue-100 p-2 rounded-md mr-3">
                                <i class="fas fa-tree text-blue-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">B-tree 索引</h4>
                        </div>
                        <p class="text-gray-600 pl-11">适用于范围查询和精确查询。大多数数据库使用 B-tree 索引来加速查询操作。</p>
                    </div>
                    <div>
                        <div class="flex items-center mb-3">
                            <div class="bg-purple-100 p-2 rounded-md mr-3">
                                <i class="fas fa-barcode text-purple-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">哈希索引</h4>
                        </div>
                        <p class="text-gray-600 pl-11">适用于精确匹配查询，不支持范围查询。</p>
                    </div>
                    <div>
                        <div class="flex items-center mb-3">
                            <div class="bg-green-100 p-2 rounded-md mr-3">
                                <i class="fas fa-search text-green-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">全文索引</h4>
                        </div>
                        <p class="text-gray-600 pl-11">适用于文本数据的全文搜索，能够对文本进行高效的搜索。</p>
                    </div>
                    <div>
                        <div class="flex items-center mb-3">
                            <div class="bg-yellow-100 p-2 rounded-md mr-3">
                                <i class="fas fa-th-large text-yellow-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">位图索引</h4>
                        </div>
                        <p class="text-gray-600 pl-11">适用于低基数的列（例如性别、状态等），可以加速查询。</p>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">索引选择原则</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="card bg-blue-50 p-6">
                        <h4 class="text-lg font-semibold mb-3 text-blue-700">应该创建索引的情况</h4>
                        <ul class="space-y-2 text-blue-800">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>主键和外键字段</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>WHERE子句中频繁使用的字段</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>ORDER BY、GROUP BY、DISTINCT字段</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>多表连接时的连接字段</span>
                            </li>
                        </ul>
                    </div>
                    <div class="card bg-red-50 p-6">
                        <h4 class="text-lg font-semibold mb-3 text-red-700">不应创建索引的情况</h4>
                        <ul class="space-y-2 text-red-800">
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>数据量小的表</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>频繁更新的字段</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>区分度低的字段(如性别)</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                                <span>很少或从不用于查询的字段</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Query Optimization Section -->
    <section id="query" class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">查询优化</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">查询语句优化</h3>
                    <div class="bg-gray-800 rounded-lg p-4 mb-4">
                        <code class="text-green-400 text-sm">
                            <span class="text-blue-300">-- 不推荐写法</span><br>
                            SELECT * FROM users WHERE age > 20 ORDER BY create_time;<br><br>
                            <span class="text-blue-300">-- 优化后写法</span><br>
                            SELECT id, name, email FROM users <br>
                            WHERE age > 20 AND status = 'active'<br>
                            ORDER BY create_time LIMIT 100;
                        </code>
                    </div>
                    <ul class="text-gray-600 space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>避免SELECT *，只查询需要的字段</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>使用合适的WHERE条件，避免全表扫描</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>添加LIMIT限制返回行数</span>
                        </li>
                    </ul>
                </div>
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">执行计划分析</h3>
                    <div class="bg-gray-100 rounded-lg p-4 mb-4">
                        <div class="flex justify-between items-center text-sm text-gray-500 mb-2">
                            <span>EXPLAIN SELECT * FROM users WHERE id = 100;</span>
                        </div>
                        <table class="min-w-full border">
                            <thead>
                                <tr class="bg-gray-200">
                                    <th class="border px-2 py-1 text-xs">id</th>
                                    <th class="border px-2 py-1 text-xs">select_type</th>
                                    <th class="border px-2 py-1 text-xs">type</th>
                                    <th class="border px-2 py-1 text-xs">key</th>
                                    <th class="border px-2 py-1 text-xs">rows</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td class="border px-2 py-1 text-xs">1</td>
                                    <td class="border px-2 py-1 text-xs">SIMPLE</td>
                                    <td class="border px-2 py-1 text-xs">const</td>
                                    <td class="border px-2 py-1 text-xs">PRIMARY</td>
                                    <td class="border px-2 py-1 text-xs">1</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <p class="text-gray-600">
                        type列显示"const"表示使用了主键索引，只需读取一行数据，是最优的执行计划。
                        如果显示"ALL"则表示全表扫描，需要优化查询或添加索引。
                    </p>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">查询缓存</h3>
                <div class="flex items-start">
                    <div class="bg-yellow-100 p-3 rounded-full mr-4 mt-1">
                        <i class="fas fa-bolt text-yellow-600"></i>
                    </div>
                    <div>
                        <p class="text-gray-600 mb-4">
                            查询缓存可以大大提高查询速度，减少数据库的负载。通过启用数据库查询缓存，常用的查询结果会被存储在内存中，下次查询时可以直接从缓存中获取结果。
                        </p>
                        <div class="bg-gray-800 rounded-lg p-4 inline-block">
                            <code class="text-green-400 text-sm">
                                <span class="text-blue-300">-- MySQL查询缓存配置示例</span><br>
                                SET GLOBAL query_cache_size = 1048576; <span class="text-gray-500">-- 1MB缓存大小</span><br>
                                SET GLOBAL query_cache_type = ON;
                            </code>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Transactions Section -->
    <section id="transactions" class="py-16 px-4 bg-gray-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">数据库事务与并发控制</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">ACID特性</h3>
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <div class="bg-blue-100 p-2 rounded-md mr-3">
                                <i class="fas fa-atom text-blue-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">原子性 (Atomicity)</h4>
                                <p class="text-gray-600">事务中的操作要么全部完成，要么全部回滚。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-green-100 p-2 rounded-md mr-3">
                                <i class="fas fa-link text-green-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">一致性 (Consistency)</h4>
                                <p class="text-gray-600">事务执行前后，数据库的数据必须保持一致。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-purple-100 p-2 rounded-md mr-3">
                                <i class="fas fa-user-lock text-purple-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">隔离性 (Isolation)</h4>
                                <p class="text-gray-600">并发事务执行时，各自的操作不会相互干扰。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-red-100 p-2 rounded-md mr-3">
                                <i class="fas fa-save text-red-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">持久性 (Durability)</h4>
                                <p class="text-gray-600">事务完成后，对数据库的修改是永久性的。</p>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">事务隔离级别</h3>
                    <div class="mermaid">
                        graph LR
                            A[隔离级别] --> B[读未提交]
                            A --> C[读已提交]
                            A --> D[可重复读]
                            A --> E[串行化]
                            B -->|问题| F[脏读]
                            C -->|问题| G[不可重复读]
                            D -->|问题| H[幻读]
                            E -->|问题| I[性能低]
                    </div>
                    <p class="text-gray-600 mt-4">
                        不同隔离级别解决了不同的问题，但隔离级别越高，并发性能越低。需要根据业务需求选择合适的隔离级别。
                    </p>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">锁机制</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-lg font-semibold mb-3">锁类型</h4>
                        <ul class="space-y-3 text-gray-600">
                            <li class="flex items-start">
                                <span class="bg-blue-100 text-blue-800 text-xs font-semibold px-2 py-1 rounded mr-2">共享锁(S锁)</span>
                                <span>读锁，多个事务可以同时持有</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-red-100 text-red-800 text-xs font-semibold px-2 py-1 rounded mr-2">排他锁(X锁)</span>
                                <span>写锁，一个事务独占资源</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-purple-100 text-purple-800 text-xs font-semibold px-2 py-1 rounded mr-2">意向锁</span>
                                <span>表示事务想在更细粒度上加锁</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-lg font-semibold mb-3">锁粒度</h4>
                        <ul class="space-y-3 text-gray-600">
                            <li class="flex items-start">
                                <span class="bg-gray-100 text-gray-800 text-xs font-semibold px-2 py-1 rounded mr-2">表锁</span>
                                <span>锁定整个表，开销小但并发低</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-gray-100 text-gray-800 text-xs font-semibold px-2 py-1 rounded mr-2">行锁</span>
                                <span>锁定特定行，开销大但并发高</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-gray-100 text-gray-800 text-xs font-semibold px-2 py-1 rounded mr-2">页锁</span>
                                <span>介于表锁和行锁之间</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Monitoring Section -->
    <section id="monitoring" class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">数据库性能监控</h2>
            
            <div class="grid md:grid-cols-3 gap-6 mb-12">
                <div class="card bg-white p-6 text-center hover-scale">
                    <div class="text-blue-500 text-5xl mb-4">
                        <i class="fas fa-chart-bar"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">Prometheus</h3>
                    <p class="text-gray-600">开源的监控系统，可以对数据库性能进行实时监控，并提供可视化的报告。</p>
                </div>
                <div class="card bg-white p-6 text-center hover-scale">
                    <div class="text-purple-500 text-5xl mb-4">
                        <i class="fas fa-chart-pie"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">New Relic</h3>
                    <p class="text-gray-600">商业监控工具，支持多种数据库的性能监控。</p>
                </div>
                <div class="card bg-white p-6 text-center hover-scale">
                    <div class="text-green-500 text-5xl mb-4">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">MySQL Monitor</h3>
                    <p class="text-gray-600">专门针对 MySQL 数据库的性能监控工具，提供全面的性能数据。</p>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">关键监控指标</h3>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-2 rounded-md mr-3">
                                <i class="fas fa-stopwatch text-blue-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">查询响应时间</h4>
                        </div>
                        <p class="text-gray-600 pl-11">衡量数据库响应查询的时间，超过阈值可能表明性能问题。</p>
                    </div>
                    <div>
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-2 rounded-md mr-3">
                                <i class="fas fa-microchip text-purple-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">CPU 和内存使用率</h4>
                        </div>
                        <p class="text-gray-600 pl-11">评估数据库在资源方面的消耗，高使用率可能需优化或扩容。</p>
                    </div>
                    <div>
                        <div class="flex items-center mb-4">
                            <div class="bg-green-100 p-2 rounded-md mr-3">
                                <i class="fas fa-hdd text-green-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">磁盘 I/O</h4>
                        </div>
                        <p class="text-gray-600 pl-11">监控数据库磁盘的读写性能，避免磁盘成为瓶颈。</p>
                    </div>
                    <div>
                        <div class="flex items-center mb-4">
                            <div class="bg-red-100 p-2 rounded-md mr-3">
                                <i class="fas fa-lock text-red-600"></i>
                            </div>
                            <h4 class="text-lg font-semibold">锁等待时间</h4>
                        </div>
                        <p class="text-gray-600 pl-11">监控数据库中事务的锁等待情况，避免死锁和锁竞争。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- HA Section -->
    <section id="ha" class="py-16 px-4 bg-gray-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">高可用性与容灾设计</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">备份策略</h3>
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <div class="bg-blue-100 p-2 rounded-md mr-3">
                                <i class="fas fa-database text-blue-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">全量备份</h4>
                                <p class="text-gray-600">定期完整备份整个数据库，恢复简单但占用空间大。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-purple-100 p-2 rounded-md mr-3">
                                <i class="fas fa-plus-circle text-purple-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">增量备份</h4>
                                <p class="text-gray-600">只备份自上次备份以来变化的数据，节省空间但恢复复杂。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-green-100 p-2 rounded-md mr-3">
                                <i class="fas fa-file-alt text-green-600"></i>
                            </div>
                            <div>
                                <h4 class="text-lg font-semibold text-gray-800">日志备份</h4>
                                <p class="text-gray-600">备份事务日志，可实现时间点恢复。</p>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="card bg-white p-6">
                    <h3 class="text-2xl font-semibold mb-4 section-title">故障转移机制</h3>
                    <div class="mermaid">
                        sequenceDiagram
                            participant C as Client
                            participant M as Master
                            participant S as Slave
                            participant H as HAProxy
                            C->>H: 请求
                            H->>M: 写请求
                            H->>S: 读请求
                            M->>S: 数据同步
                            Note right of M: 主库故障
                            S->>H: 心跳检测失败
                            H->>S: 提升为新的主库
                            H->>C: 重定向请求到新主库
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h3 class="text-2xl font-semibold mb-6 section-title">灾难恢复计划</h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="border-l-4 border-blue-500 pl-4">
                        <h4 class="text-lg font-semibold mb-2 text-blue-700">RTO (恢复时间目标)</h4>
                        <p class="text-gray-600">系统允许的最大停机时间，通常以小时或分钟计。</p>
                    </div>
                    <div class="border-l-4 border-purple-500 pl-4">
                        <h4 class="text-lg font-semibold mb-2 text-purple-700">RPO (恢复点目标)</h4>
                        <p class="text-gray-600">系统允许丢失的最大数据量，通常以时间或事务数量计。</p>
                    </div>
                    <div class="border-l-4 border-green-500 pl-4">
                        <h4 class="text-lg font-semibold mb-2 text-green-700">测试与演练</h4>
                        <p class="text-gray-600">定期测试恢复流程，确保灾难发生时能按计划执行。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Conclusion Section -->
    <section class="py-16 px-4">
        <div class="container mx-auto max-w-3xl text-center">
            <div class="bg-white rounded-xl p-8 shadow-lg">
                <div class="text-blue-600 text-5xl mb-6">
                    <i class="fas fa-check-circle"></i>
                </div>
                <h2 class="text-3xl font-bold mb-4">数据库设计与优化总结</h2>
                <p class="text-gray-600 mb-6">
                    数据库设计与优化是一个持续的过程，需要根据业务发展不断调整。从合理的数据模型设计，到索引优化、查询优化，再到架构扩展和容灾设计，每一环节都影响着系统的性能和稳定性。
                </p>
                <p class="text-gray-600">
                    掌握这些核心原则和实践方法，能够帮助您构建高效、可靠的数据存储系统，为业务发展提供坚实的技术基础。
                </p>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h3 class="text-xl font-semibold mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300">
                    <i class="fas fa-globe mr-2"></i>www.yuque.com/jtostring
                </a>
                <div class="mt-6 flex space-x-6">
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-github text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-twitter text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-linkedin text-xl"></i>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>